package com.lb.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @Description: zip文件解压
 * @Author: LB
 * @Date: 2021/6/4 17:24
 **/
public class UnZipFile
{
  /**
   * 解压文件到指定目录
   */
  @SuppressWarnings("rawtypes")
  public static void unZipFiles(File zipFile,String descDir)throws IOException
  {
    File pathFile = new File(descDir);
    if(!pathFile.exists())
    {
      //不存在创建文件夹
      pathFile.mkdirs();
    }
    //解决zip文件中有中文目录或者中文文件
    ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));

    Enumeration<? extends ZipEntry> enumeration = zip.entries();
    while (enumeration.hasMoreElements()){
      ZipEntry entry = enumeration.nextElement();
      if(entry.isDirectory()){
        continue;
      }
      String[] split = entry.getName().split("/");
      String fileName = split[split.length-1];
      InputStream in = zip.getInputStream(entry);
      String outPath = (descDir+File.separator+fileName);
      //输出文件路径信息
      System.out.println(outPath);
      OutputStream out = new FileOutputStream(outPath);
      byte[] buf1 = new byte[1024];
      int len;
      while((len=in.read(buf1))>0)
      {
        out.write(buf1,0,len);
      }
      in.close();
      out.close();
    }
    //TODO:解压完的工具人可以删除了
    System.out.println("******************解压完毕********************");
  }
  public static void main(String[] args) throws IOException {
    /**
     * 解压文件
     */
    File zipFile = new File("F:/photo.zip");
    String path = "F:/zipfile/";
    unZipFiles(zipFile, path);
  }
}